package com.misc;

import java.util.Timer;
import java.util.Vector;

import ShareLife.gar.CalculateValue;
import ShareLife.gar.ContactsLayer;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;



public class ContactLayerManager implements AnimationStepListener {

	private Vector <ContactsLayer> contactlayers;
	//private int layerindex = 0;
	private int focusindex = 0;
	final static int LayerWidth = 350;
	final static int LEFT = 0;
	final static int RIGHT = 1;
	final static int UP = 2;
	final static int DOWN = 3;
	private int layerposition[] = null;
	private View container = null;
	private boolean Running = false;
	private boolean Left = false;
	//private boolean Up = false;

	public ContactLayerManager(View aContainer) {
		container = aContainer;
		contactlayers = new Vector<ContactsLayer>();

	}

	public void drawAll(Canvas aGraphics, Paint aPaint) {
		for (int i = 0; i < contactlayers.size(); i++) {
			ContactsLayer tempLayer = contactlayers
					.elementAt(i);
			if (tempLayer.getX() < -LayerWidth || tempLayer.getX() > 400)
				continue;
			tempLayer.drawLayer(aGraphics, aPaint);
		}
	}

	public void runManager() {

	}
	public int getindex()
	{
		return focusindex;
		
	}
	public void addlayer(ContactsLayer aLayer) {
		if (focusindex != 0) {
			aLayer.setX((contactlayers.size() - focusindex) * LayerWidth);
		} else {
			aLayer.setX(contactlayers.size() * LayerWidth);
		}
		contactlayers.addElement(aLayer);
	}
	
	public ContactsLayer getContact(int aIndex)
	{
		if(aIndex <0 || aIndex >= contactlayers.size())
		{
			return null;
		}
		return 	contactlayers.elementAt(aIndex);
	}
	public void numberPosition() {
		if (layerposition != null) {
			layerposition = null;
		}
		layerposition = new int[contactlayers.size()];
	}

	public void directionHandleLAndR(int aDirection) {
		if (Running)
			return;
		Running = true;
		int numberAnimation = contactlayers.size();
		int start[] = new int[numberAnimation];
		int end[] = new int[numberAnimation];
		switch (aDirection) {
		case LEFT:
			handleLeft(start, end, numberAnimation);
			break;
		case RIGHT:
			handleRight(start, end, numberAnimation);
			break;
		case UP:
			handleUp();
			break;
		case DOWN:
			handleDown();
			break;
		}
		AnimationTimerTask tempvalue = null;
		Timer temptimer = new Timer();
		CalculateValue.ECurveType typecurve = CalculateValue.ECurveType.OutBack;
		tempvalue = new AnimationTimerTask(start, end, typecurve, layerposition,
				numberAnimation, 800, 50,temptimer, this);
		temptimer.schedule(tempvalue, 0, 100);
	}

	private void handleLeft(int[] aStart, int[] aEnd, int aNumber) {
		if (focusindex >= aNumber - 1) {
			Running = false;
			return;
		}
		for (int i = 0; i < aNumber; i++) {
			ContactsLayer tempLayer = contactlayers
					.elementAt(i);
			aStart[i] = tempLayer.getX();
			aEnd[i] = tempLayer.getX() - LayerWidth;
		}
		Left = true;

	}

	private void handleRight(int[] aStart, int[] aEnd, int aNumber) {

		if (focusindex <= 0) {
			Running = false;
			return;
		}

		for (int i = 0; i < aNumber; i++) {
			ContactsLayer tempLayer = contactlayers
					.elementAt(i);
			aStart[i] = tempLayer.getX();
			aEnd[i] = tempLayer.getX() + LayerWidth;
		}
	}

	private void handleUp() {

	}

	private void handleDown() {

	}

	public void AnimationStep() {
		// TODO Auto-generated method stub
		for (int i = 0; i < contactlayers.size(); i++) {
			ContactsLayer tempLayer = contactlayers
					.elementAt(i);
			tempLayer.setX(layerposition[i]);
		}
		container.postInvalidate();
	}

	public void AnimationFinished() {
		// TODO Auto-generated method stub
		if (Left) {
			focusindex++;
			Left = false;
		} else {
			focusindex--;
		}
		Running = false;
		container.postInvalidate();
	}
	public int getNumber()
	{
		return contactlayers.size();
	}
	public void initalData()
	{
		for(int i=0; i < contactlayers.size();i++)
		{
			ContactsLayer layer = contactlayers.elementAt(i);
			layer.initalLocation();	
		}
	}
}
